package com.budgetme.widgets;

import com.budgetme.R;
import com.budgetme.database.Database;
import com.budgetme.ui.fragments.BudgetDetailsFragment;
import com.budgetme.ui.fragments.BudgetListFragment;
import com.budgetme.ui.fragments.BudgetListFragment.OnListItemSelectedListener;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.appwidget.AppWidgetManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.AdapterView.OnItemClickListener;


public class BudgetWidgetConfigure extends Activity implements OnListItemSelectedListener {
	private static final String TAG = BudgetWidgetConfigure.class.getName();
	
	public static final String PREFS_NAME = "com.budgetme.widgets.BucketWidget";
	public static final String PREFS_KEY_BUCKET_ID = "bucket_id_";

	private EditText mEditText;
	private Button mButton;
	
	private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
	
	private ContentResolver mContentResolver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Set the result to CANCELED.  This will cause the widget host to cancel
        // out of the widget placement if they press the back button.
        setResult(RESULT_CANCELED);

        setContentView(R.layout.budget_widget_configure);
        
        // Find the widget id from the intent. 
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
        	mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        // If they gave us an intent without the widget id, just bail.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
        }
		
		BudgetListFragment fragment = new BudgetListFragment();
		
		Bundle args = new Bundle();
		args.putInt(BudgetListFragment.ARG_FILTER, Database.tbl_budgets.FILTER_ENABLED);
		fragment.setArguments(args);
		fragment.setOnListItemSelectedListener(this);
		
		FragmentTransaction transaction = getFragmentManager().beginTransaction();
		transaction.add(R.id.fragment_container, fragment).commit();
        
    }

	@Override
	public void onItemSelected(int id) {

      // Save the bucket ID in shared preferences
      SharedPreferences.Editor prefs = getSharedPreferences(PREFS_NAME, 0).edit();
      prefs.putInt(PREFS_KEY_BUCKET_ID + mAppWidgetId, id);
      prefs.commit();
      
      // Push widget update to surface with newly set prefix
      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
      BudgetWidget.updateBudgetWidget(this, appWidgetManager, mAppWidgetId);
      
      // Make sure we pass back the original appWidgetId
      Intent resultValue = new Intent();
      resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
      setResult(RESULT_OK, resultValue);
      finish();
		
	}

//	@Override
//	public void onBudgetSelected(int id) {
//

//		
//	}

//	@Override
//	public void onAddBudgetSelected() {
//		// TODO Auto-generated method stub
//		
//	}

}
